package com.idea.tianmimi;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabWidget;

public class MainTabActivity extends BaseTabActivity 
{
	private TabHost tabHost;
	private TabWidget tabWidget;
	private String lastTabTag = null;
	private int lastTabTab = 0;
	
	
	LayoutInflater inflater;

	public static final String NEARBY_TAG = "附近";
	public static final String EMAIL_TAG = "信箱";
	public static final String FRIEND_TAG = "好友";
	public static final String GIFT_TAG = "礼物";
	public static final String ME_TAG = "我";

	public static final int NEARBY_TAB = 0;
	public static final int EMAIL_TAB = 1;
	public static final int FRIEND_TAB = 2;
	public static final int GIFT_TAB = 3;
	public static final int ME_TAB = 4;

	private static Map<Integer, Integer> resMapping;
	private static Map<String, Integer> tabMapping;

	static {
		resMapping = new HashMap<Integer, Integer>();
		resMapping.put(NEARBY_TAB, R.drawable.tab_item);
		resMapping.put(EMAIL_TAB, R.drawable.tab_item);
		resMapping.put(FRIEND_TAB, R.drawable.tab_item);
		resMapping.put(GIFT_TAB, R.drawable.tab_item);
		resMapping.put(ME_TAB, R.drawable.tab_item);

		tabMapping = new HashMap<String, Integer>();
		tabMapping.put(NEARBY_TAG, NEARBY_TAB);
		tabMapping.put(EMAIL_TAG, EMAIL_TAB);
		tabMapping.put(FRIEND_TAG, FRIEND_TAB);
		tabMapping.put(GIFT_TAG, GIFT_TAB);
		tabMapping.put(ME_TAG, ME_TAB);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setTheme(android.R.style.Theme_Translucent_NoTitleBar);
		setContentView(R.layout.main_tab);

		tabHost = getTabHost();
		tabWidget = tabHost.getTabWidget();

		
		
		addNewTab(R.drawable.tab_item, NEARBY_TAG,
				NearbyActivity.class);
		addNewTab(R.drawable.tab_item, EMAIL_TAG,
				NearbyActivity.class);
		addNewTab(R.drawable.tab_item, FRIEND_TAG,
				NearbyActivity.class);
		addNewTab(R.drawable.tab_item, GIFT_TAG,
				NearbyActivity.class);
		addNewTab(R.drawable.tab_item, ME_TAG,
				NearbyActivity.class);

		tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			public void onTabChanged(String tag) {
				changeTab(tag);
			}
		});

		tabWidget.getChildAt(NEARBY_TAB).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
//						if (NEARBY_TAB == lastTabTab) {
//							((TabItemBaseActivity) getLocalActivityManager()
//									.getCurrentActivity())
//									.onCurrentTabButtonClick();
//						}
						tabHost.setCurrentTab(NEARBY_TAB);
					}
				});

		tabWidget.getChildAt(EMAIL_TAB).setOnClickListener(
				new OnClickListener() {

					public void onClick(View v) {

//						if (COMMENT_TAB == lastTabTab) {
//							((TabItemBaseActivity) getLocalActivityManager()
//									.getCurrentActivity())
//									.onCurrentTabButtonClick();
//						}
						tabHost.setCurrentTab(EMAIL_TAB);
					}
				});
		tabWidget.getChildAt(FRIEND_TAB).setOnClickListener(
				new OnClickListener() {

					public void onClick(View v) {

//						if (COMMENT_TAB == lastTabTab) {
//							((TabItemBaseActivity) getLocalActivityManager()
//									.getCurrentActivity())
//									.onCurrentTabButtonClick();
//						}
						tabHost.setCurrentTab(FRIEND_TAB);
					}
				});
		tabWidget.getChildAt(GIFT_TAB).setOnClickListener(
				new OnClickListener() {

					public void onClick(View v) {

//						if (COMMENT_TAB == lastTabTab) {
//							((TabItemBaseActivity) getLocalActivityManager()
//									.getCurrentActivity())
//									.onCurrentTabButtonClick();
//						}
						tabHost.setCurrentTab(GIFT_TAB);
					}
				});
		tabWidget.getChildAt(ME_TAB).setOnClickListener(
				new OnClickListener() {

					public void onClick(View v) {

//						if (COMMENT_TAB == lastTabTab) {
//							((TabItemBaseActivity) getLocalActivityManager()
//									.getCurrentActivity())
//									.onCurrentTabButtonClick();
//						}
						tabHost.setCurrentTab(ME_TAB);
					}
				});
		
		changeTab(NEARBY_TAG);

	}

	public final void changeTab(String tag) {

		int tabId = (int) tabMapping.get(tag);

		Button lastView = (Button) tabWidget.getChildAt(lastTabTab);
		lastView.setBackgroundResource(resMapping.get(lastTabTab));

		Button currentView = (Button) tabWidget.getChildAt(tabId);
		currentView.setBackgroundResource(resMapping.get(tabId));

		
		
		switch (tabId) {
		case NEARBY_TAB:
//			currentView
//					.setBackgroundResource(R.drawable.uni_button_mianbar_l_on);
			tabHost.setCurrentTab(NEARBY_TAB);
			break;
		case EMAIL_TAB:
//			currentView
//					.setBackgroundResource(R.drawable.uni_button_mianbar_m_on);
			tabHost.setCurrentTab(EMAIL_TAB);
			break;
		case FRIEND_TAB:
//			currentView
//					.setBackgroundResource(R.drawable.uni_button_mianbar_r_on);
			tabHost.setCurrentTab(FRIEND_TAB);
			break;
		case GIFT_TAB:
//			currentView
//					.setBackgroundResource(R.drawable.uni_button_mianbar_r_on);
			tabHost.setCurrentTab(GIFT_TAB);
			break;
		case ME_TAB:
//			currentView
//					.setBackgroundResource(R.drawable.uni_button_mianbar_r_on);
			tabHost.setCurrentTab(ME_TAB);
			break;
		}
		lastTabTab = tabId;
		lastTabTag = tag;
	}

	
}
